package xhwl.insurance.ui;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.RequiresApi;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import de.greenrobot.event.EventBus;
import rx.functions.Action1;
import xhwl.android.common.log.XHLog;
import xhwl.android.common.util.SPUtil;
import xhwl.android.common.util.ToastUtil;
import xhwl.insurance.BaseActivity;
import xhwl.insurance.R;
import xhwl.insurance.adapter.ForumCommentListAdapter;
import xhwl.insurance.customView.CircleImageView;
import xhwl.insurance.customView.PopSelectPic;
import xhwl.insurance.event.ClickForumNoPicEvent;
import xhwl.insurance.event.FreshForumListEvent;
import xhwl.insurance.event.FreshPicEvent;
import xhwl.insurance.event.SendVoiceEvent;
import xhwl.insurance.event.StopVoiceEvent;
import xhwl.insurance.http.api.CommentListApi;
import xhwl.insurance.http.api.SendForumCommentApi;
import xhwl.insurance.http.api.UploadPicApi;
import xhwl.insurance.http.api.UploadVoiceApi;
import xhwl.insurance.http.bean.ImageEvent;
import xhwl.insurance.http.bean.ImageItem;
import xhwl.insurance.http.bean.RecordItem;
import xhwl.insurance.http.bean.ReferralPicBean;
import xhwl.insurance.http.bean.resp.ForumCommentResp;
import xhwl.insurance.http.bean.resp.ForumProductResp;
import xhwl.insurance.http.bean.resp2.HttpResp;
import xhwl.insurance.morepic.adapter.ImagePublishAdapter;
import xhwl.insurance.morepic.adapter.util.CustomConstants;
import xhwl.insurance.morepic.adapter.util.IntentConstants;
import xhwl.insurance.ui.voicesupport.AudioButton;
import xhwl.insurance.ui.voicesupport.MediaPlay;
import xhwl.insurance.util.AndroidBug5497Workaround;
import xhwl.insurance.util.ImgTools;
import xhwl.insurance.util.KeyBoardUtils;
import xhwl.insurance.util.PermisionUtils;
import xhwl.insurance.view.AudioButton2;
import xhwl.insurance.view.ForumPictureView;
import xhwl.insurance.view.NoScrollListView;
import xhwl.retrofitrx.BaseApi;
import xhwl.retrofitrx.HttpException;
import xhwl.retrofitrx.HttpManager;
import xhwl.retrofitrx.OnNextListener;

/**
 * Created by Administrator on 2017/10/9.
 * 论坛详情页(新版)
 */

public class ForumDetailNewActivity extends BaseActivity implements View.OnClickListener, PopSelectPic.PopSelectBtnClickListener {
    //    private static String[] PERMISSION = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private static final String TAG = ForumDetailNewActivity.class.getSimpleName();
    private static final int TAKE_PICTURE = 0x000000;
    private static final int REQUEST_CODE_VIEW_PIC = 0x000001;
    public static final String EXTRA_DATA = "data";

    private ListView mListView;
    public List<ImageItem> mDataList = new ArrayList<ImageItem>();
    List<ForumCommentResp> CommentList = new ArrayList<>();
    private ForumCommentListAdapter mForumCommentListAdapter;

    private EditText commentEdit;
    private Button commentButton;

    private ImageButton mAddPic;
    private ImagePublishAdapter mAdapter;
    private GridView mGridView;

    private ScrollView mScrollViewOne;
    private LinearLayout mLinearLayoutOne;

    private String reply_details = ""; //记录对话框中的内容
    private String path = "";
    private ForumProductResp data;
    String mPage = "1";
    private ImageView mic;
    private ImageView consult_chat_text;
    private LinearLayout ll_audiobutton;
    private AudioButton2 mAudioButton;
    private int times;
    private RecordItem item;
    private File file;
    private String voiceUrl;
    private String reply_voice;
    private String voice_long;
    private View commentListAnimeView;
    private ImageView mFourumDetailView;
    private AnimationDrawable ad;
    private AnimationDrawable ad1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        data = (ForumProductResp) getIntent().getSerializableExtra(EXTRA_DATA);
        setContentView(R.layout.activity_new_forum_detail);
        AndroidBug5497Workaround.assistActivity(this);

    }

    @Override
    public void initViews() {
        initTitleViews();
        initCommentView();

        //评论数据list
        mListView = (ListView) findViewById(R.id.lv_pinglun);
        mForumCommentListAdapter = new ForumCommentListAdapter(ForumDetailNewActivity.this, CommentList);
        mListView.setAdapter(mForumCommentListAdapter);

        //点击下面的+号和EditText和添加图片的gridVeiw
        mAddPic = (ImageButton) findViewById(R.id.emotion_add);
        commentEdit = (EditText) findViewById(R.id.commentEdit);
        commentButton = (Button) findViewById(R.id.commentButton);//发送

        mGridView = (GridView) findViewById(R.id.gridview);
        mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        mAdapter = new ImagePublishAdapter(this, mDataList);
        mGridView.setAdapter(mAdapter);


        mScrollViewOne = (ScrollView) findViewById(R.id.sll_one);//上部分布局
        mLinearLayoutOne = (LinearLayout) findViewById(R.id.ll_one);  //上部分布局
        NoScrollListView commentList = (NoScrollListView) findViewById(R.id.lv_pinglun);//评论列表
        commentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mGridView.setVisibility(View.GONE);
                mic.setVisibility(View.VISIBLE);

            }
        });

        /**
         * 判断底部EditText显示情况
         */
        if ("".equals(reply_details) && mDataList.size() != 0) {
            commentButton.setVisibility(View.VISIBLE);
        } else if (mDataList.size() == 0 && !reply_details.equals("")) {
            mGridView.setVisibility(View.GONE);
        } else if (("".equals(reply_details) && mDataList.size() == 0)) {
            mGridView.setVisibility(View.GONE);
        }
/**
 * 点击单个item中的语音停止动画
 */
        mForumCommentListAdapter.setListItemBtnClickListener(new ForumCommentListAdapter.ListItemBtnClickListener() {
            @Override
            public void onClick(int position, View convertView, ViewGroup parent,String reply_voice) {
                if (commentListAnimeView != null) {
                    commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);
                    commentListAnimeView = null;
                }
                commentListAnimeView = convertView.findViewById(R.id.v_anime);
                commentListAnimeView.setBackgroundResource(R.drawable.voice_forum_animation);
                ad = (AnimationDrawable) commentListAnimeView.getBackground();
                ad.start();
                //播放录制的音频
                MediaPlay.playAudio(reply_voice, new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        //播放完成，停止动画效果
                        commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);

                    }
                });
            }
        });

    }
    private final int MOBILE_QUERY = 1;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MOBILE_QUERY:
                    mForumCommentListAdapter = new ForumCommentListAdapter(ForumDetailNewActivity.this, CommentList);
                    mListView.setAdapter(mForumCommentListAdapter);
                    mForumCommentListAdapter.setListItemBtnClickListener(new ForumCommentListAdapter.ListItemBtnClickListener() {
                        @Override
                        public void onClick(int position, View convertView, ViewGroup parent,String reply_voice) {
                            if (commentListAnimeView != null) {
                                commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);
                                commentListAnimeView = null;
                            }
                            commentListAnimeView = convertView.findViewById(R.id.v_anime);
                            commentListAnimeView.setBackgroundResource(R.drawable.voice_forum_animation);
                            ad = (AnimationDrawable) commentListAnimeView.getBackground();
                            ad.start();
                            //播放录制的音频
                            MediaPlay.playAudio(reply_voice, new MediaPlayer.OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    //播放完成，停止动画效果
                                    commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);
                                }
                            });
                        }
                    });
                    break;
            }
        }
    };
    private void initCommentView() {
        TextView tv_forum_title = (TextView) findViewById(R.id.tv_forum_title);//论坛名
        TextView tv_name = (TextView) findViewById(R.id.tv_name);//姓名
        TextView tv_forum_time = (TextView) findViewById(R.id.tv_forum_time);//时间
        CircleImageView img_header = (CircleImageView) findViewById(R.id.img_header);//头像
        TextView tv_content = (TextView) findViewById(R.id.tv_content);//内容
        ForumPictureView forumPicView = (ForumPictureView) findViewById(R.id.forumPicView);
        LinearLayout ll_voice = (LinearLayout) findViewById(R.id.ll_voice);//语音布局
        ImageView voice_display_voice_play = (ImageView) findViewById(R.id.voice_display_voice_play);
        TextView voice_display_voice_time = (TextView) findViewById(R.id.voice_display_voice_time);//语音时长
        if (data.getVOICE_LONG() != null) {
            voice_display_voice_time.setText(data.getVOICE_LONG() + "\"");//语音时长
        } else {
            voice_display_voice_time.setText("未知");
        }
        if ("1".equals(data.getPOST_TYPE())) {
            ll_voice.setVisibility(View.VISIBLE);
            voice_display_voice_play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mFourumDetailView = (ImageView) findViewById(R.id.v_anime);
                    //点击帖子内容中的语音停止评论列表动画
                    Message msg =Message.obtain();
                    msg.what= MOBILE_QUERY;
                    mHandler.sendMessage(msg);
                  //刷新评论列表
                    mFourumDetailView.setBackgroundResource(R.drawable.voice_forum_animation);
                    ad1= (AnimationDrawable) mFourumDetailView.getBackground();
                    ad1.start();
                    //播放录制的音频
                    MediaPlay.playAudio(data.getVI_PI_URL(), new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            //播放完成，停止动画效果
                            mFourumDetailView.setBackgroundResource(R.drawable.adj_voice);
                        }
                    });
                }
            });
        } else {
            ll_voice.setVisibility(View.GONE);
        }
        if ("0".equals(data.getPOST_TYPE())) {
            forumPicView.setData(data.getVI_PI_URL(), Integer.MAX_VALUE);
        }
        forumPicView.setData2(data);

        tv_forum_title.setText(data.getPOST_TITLE());
        tv_name.setText(data.getMEMBER_NAME());
        tv_forum_time.setText(data.getISSUE_DATE());
        tv_content.setText(data.getPOST_DETAILS());
        ImgTools.getInstance().getImgFromNetByUrl(data.getMEMBER_PIC(), img_header, R.drawable.touxiang);//头像


        //添加小话筒布局
        mic = (ImageView) findViewById(R.id.consult_chat_mic);
        consult_chat_text = (ImageView) findViewById(R.id.consult_chat_text);
        ll_audiobutton = (LinearLayout) findViewById(R.id.ll_audiobutton);
        mAudioButton = (AudioButton2) findViewById(R.id.audiobutton_btn);
        mAudioButton.setOnStartListener(new AudioButton.onStartListener() {
            @Override
            public void startRecord() {
                MediaPlay.stop();

            }
        });
        mAudioButton.setOnFinishListener(new AudioButton.onFinishListener() {


            @Override
            public void finishRecord(String path, float time) {
                item = new RecordItem(path, (int) time);
                times = item.getTime();
            }
        });
    }

    @Override
    protected void setUpTitleView() {
        super.setUpTitleView();
        titleTextView.setText(data.getPOST_TITLE());
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initListener() {
        mAddPic.setOnClickListener(this);
        mLinearLayoutOne.setOnClickListener(this);
        commentButton.setOnClickListener(this);
        commentEdit.setOnClickListener(this);
        mic.setOnClickListener(this);
        consult_chat_text.setOnClickListener(this);


        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == mDataList.size()) {
                    PopSelectPic popfront = new PopSelectPic(ForumDetailNewActivity.this, mGridView);
                    popfront.setCallBack(ForumDetailNewActivity.this);
                } else {
//                    Intent intent = new Intent(ForumDetailNewActivity.this, ImageZoomActivity.class);
//                    intent.putExtra(IntentConstants.EXTRA_IMAGE, mDataList.get(position));
//                    intent.putExtra(IntentConstants.EXTRA_CURRENT_IMG_POSITION, position);
//                    startActivityForResult(intent,REQUEST_CODE_VIEW_PIC);

                }
            }
        });
//        mScrollViewOne.setOnScrollChangeListener(new View.OnScrollChangeListener() {
//            @Override
//            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//                mGridView.setVisibility(View.GONE);
//                mic.setVisibility(View.VISIBLE);
//            }
//        });

    }

    @Override
    public void loadData() {
        //加载评论list
        getCommentData("1");

    }


    private void notifyDataChanged() {
        mAdapter.notifyDataSetChanged();
    }

    //图片处理开始
    private int getAvailableSize() {
        int availSize = CustomConstants.FORUM_MAX_IMAGE_SIZE - mDataList.size();
        if (availSize >= 0) {
            return availSize;
        }
        return 0;
    }

    @Override
    public void onTakePhotoClick() {
        takePhoto();
    }

    @Override
    public void onSelectPicClick() {
        pickPicture();
    }

    private void pickPicture() {
        //2017.11.21权限替换 后期测试没有问题删除注释liujin
        requestPermission();
    }

    private void requestPermission() {
        new RxPermissions(this).request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean granted) {
                if (granted) {
                    Intent intent = new Intent(mContext, ImageBucketChooseActivity.class);
                    intent.putExtra(xhwl.insurance.util.IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, 1);
                    intent.putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, getAvailableSize());
                    startActivity(intent);
                } else {
                    Log.e(TAG, "用户拒绝开启权限");
                    PermisionUtils.dialog(ForumDetailNewActivity.this, "用户拒绝开启权限");
                }
            }
        });


    }

    private void requestVoicePermission() {
        new RxPermissions(this).request(Manifest.permission.RECORD_AUDIO).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean granted) {
                if (granted) {
                } else {
                    PermisionUtils.dialog(ForumDetailNewActivity.this, "用户拒绝开启录音权限");
                }
            }
        });


    }

    public void takePhoto() {
        requestTakePicPermission();
    }

    private void requestTakePicPermission() {
        new RxPermissions(this).request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean granted) {
                if (granted) {
                    Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    File vFile = new File(Environment.getExternalStorageDirectory() + "/myimage/", String.valueOf(System.currentTimeMillis()) + ".jpg");
                    if (!vFile.exists()) {
                        File vDirPath = vFile.getParentFile();
                        vDirPath.mkdirs();
                    } else {
                        if (vFile.exists()) {
                            vFile.delete();
                        }
                    }
                    path = vFile.getPath();
                    Uri cameraUri;
                    if (Build.VERSION.SDK_INT >= 24) {
                        cameraUri = FileProvider.getUriForFile(getApplicationContext(), "xhwl.insurance.fileProvider", vFile);
                    } else {
                        cameraUri = Uri.fromFile(vFile);
                    }
                    openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraUri);
                    startActivityForResult(openCameraIntent, TAKE_PICTURE);

                } else {
                    Log.e(TAG, "请打开提示对应权限,否则将影响拍照功能无法使用");
                    PermisionUtils.dialog(ForumDetailNewActivity.this, "请打开提示对应权限,否则将影响拍照功能无法使用");
                }
            }
        });
    }

    public void onEvent(ImageEvent event) {
        if (event == null)
            return;

        Collection<ImageItem> collection = event.getCollection();

        for (ImageItem item : collection) {// 目前只能选择一张图片，可上传多张图片时，需根据接口定义修改s
            XHLog.e("sourcePath=" + item.getSourcePath());
            initImageFiles(item);
        }
    }

    public void onEvent(FreshPicEvent event) {
        mAdapter.notifyDataSetChanged();

    }
    public void onEvent(StopVoiceEvent event) {
        mFourumDetailView.setBackgroundResource(R.drawable.adj_voice);
    }

    public void onEvent(SendVoiceEvent event) {
        file = new File(item.getPath());
        //先发送语音得到语音地址
        if (!TextUtils.isEmpty(item.getPath()) && null != item.getPath() && "" != item.getPath()) {
            UploadVoiceApi api = new UploadVoiceApi(ForumDetailNewActivity.this, new File[]{file});
            HttpManager.getInstance().dealHttp(ForumDetailNewActivity.this, api, new OnNextListener<ArrayList<ReferralPicBean>>() {


                @Override
                public void onNext(ArrayList<ReferralPicBean> list) {
                    voiceUrl = list.get(0).getUrl_big();
                    voice_long = String.valueOf(item.getTime());
                    if (voiceUrl != null) {
                        //发表评论接口
                        SendForumComment("");
                    }
                }
            });
        } else {
            Toast.makeText(mContext, "发表的语音内容不能为空", Toast.LENGTH_SHORT).show();
        }

    }

    public void onEvent(ClickForumNoPicEvent event) {
        if (event == null)
            return;
        mGridView.setVisibility(View.GONE);
        mic.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == TAKE_PICTURE) {
                ImageItem item = new ImageItem();
                item.sourcePath = path;
                XHLog.e("sourcePath=" + item.sourcePath);
                initImageFiles(item);
            }
            //处理删除图片逻辑
            else if (requestCode == REQUEST_CODE_VIEW_PIC) {
                if (data != null) {
                    int delPos = data.getIntExtra(IntentConstants.EXTRA_CURRENT_IMG_POSITION, 0);
                    mDataList.remove(delPos);
                    notifyDataChanged();
                }
            }
        }

    }

    private void initImageFiles(ImageItem item) {
        String fileName = xhwl.insurance.util.BitmapUtil.handleImage(mContext, item.sourcePath, 200);
        File file = new File(fileName);
        mDataList.add(item);
        notifyDataChanged();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.emotion_add:
                //点击+
                mGridView.setVisibility(View.VISIBLE);
                mic.setVisibility(View.GONE);
                break;
            case R.id.ll_one:
                mGridView.setVisibility(View.GONE);
                mic.setVisibility(View.VISIBLE);
                KeyBoardUtils.hideKeyBoard(this, commentEdit);
                break;
            case R.id.commentButton:
                //发送评论
                voiceUrl = "";
                if (mDataList.size() == 0) {
                    reply_details = commentEdit.getText().toString().trim();
                    if (!reply_details.equals("")) {
                        SendForumComment("");
                    } else {
                        Toast.makeText(mContext, "回复详情不能为空", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    List<File> list = new ArrayList<>();
                    for (ImageItem item : mDataList) {
                        File file = new File(item.getSourcePath());
                        if (file != null && file.exists()) {
                            list.add(file);
                        }
                    }
                    uploadPic(list);
                }

                break;
            case R.id.consult_chat_mic://语音
                KeyBoardUtils.hideKeyBoard(this, commentEdit);
                consult_chat_text.setVisibility(View.VISIBLE);
                mic.setVisibility(View.GONE);
                commentEdit.setVisibility(View.GONE);
                commentEdit.setText("");
                mAddPic.setVisibility(View.INVISIBLE);
                mGridView.setVisibility(View.GONE);
                ll_audiobutton.setVisibility(View.VISIBLE);
                commentButton.setVisibility(View.GONE);
                //开启语音权限
                requestVoicePermission();
                break;
            case R.id.consult_chat_text://文字
                consult_chat_text.setVisibility(View.GONE);
                commentEdit.setVisibility(View.VISIBLE);
                ll_audiobutton.setVisibility(View.GONE);
                mic.setVisibility(View.VISIBLE);
                mAddPic.setVisibility(View.VISIBLE);
                commentButton.setVisibility(View.VISIBLE);
                KeyBoardUtils.hideKeyBoard(this, commentEdit);
                break;
            default:
                break;
        }

    }

    private void uploadPic(final List<File> fileList) {
        if (fileList == null || fileList.size() == 0)
            return;

        final UploadPicApi api = new UploadPicApi(mContext, true, fileList.toArray(new File[fileList.size()]));
        HttpManager.getInstance().dealHttp(this, api, new OnNextListener<ArrayList<ReferralPicBean>>() {
            @Override
            public void onStart() {
                super.onStart();

            }

            @Override
            public void onNext(ArrayList<ReferralPicBean> picBeanList) {
                api.hideDialog();
                StringBuilder sb = new StringBuilder();
                for (ReferralPicBean bean : picBeanList) {
                    sb.append(bean.getUrl_big()).append(";");
                }
                SendForumComment(sb.toString());
            }

            @Override
            public void onError(HttpException ex) {
                super.onError(ex);
                api.hideDialog();
            }
        });
    }

    /**
     * 发表评论
     */
    private void SendForumComment(String reply_annex) {
        String member_id = SPUtil.getPersonInfoSp().getString(SPUtil.SPPersonInfo.MEMBER_ID, "");
        String reply_details = commentEdit.getText().toString().trim();
        String post_id = data.getPOST_ID();
        if (voiceUrl != null && voiceUrl != "") {
            reply_voice = voiceUrl;
        } else {
            reply_voice = "";
        }
        if (voice_long != null && voice_long != "") {
            voice_long = String.valueOf(item.getTime());
        } else {
            voice_long = "";
        }
        BaseApi api = new SendForumCommentApi(member_id, post_id, reply_details, reply_annex, reply_voice, voice_long);
        HttpManager.getInstance().dealHttp(ForumDetailNewActivity.this, api, new OnNextListener<HttpResp>() {

            @Override
            public void onNext(HttpResp resp) {
                if (resp.isOk()) {
                    ToastUtil.showToast(ForumDetailNewActivity.this, "已发送");
                    getCommentData("1");
                    mDataList.clear();
                    mGridView.setVisibility(View.GONE);
                    commentEdit.setText("");
                    mAdapter.notifyDataSetChanged();
                    EventBus.getDefault().post(new FreshForumListEvent());
                } else {
                    ToastUtil.showToast(mContext, resp.message);
                }
            }

        });
    }

    /**
     * 获取评论内容
     */
    private void getCommentData(final String page) {
        String post_id = data.getPOST_ID();
        BaseApi api = new CommentListApi(page, post_id);
        HttpManager.getInstance().dealHttp(ForumDetailNewActivity.this, api, new OnNextListener<HttpResp<ArrayList<ForumCommentResp>>>() {
            @Override
            public void onNext(HttpResp<ArrayList<ForumCommentResp>> arrayListHttpResp) {
                if (arrayListHttpResp != null) {
                    if (arrayListHttpResp.isOk()) {
                        mPage = "1";
                        CommentList.clear();
                        if (arrayListHttpResp.data != null && arrayListHttpResp.data.size() > 0) {
                            CommentList.addAll(arrayListHttpResp.data);
                        }
                        mForumCommentListAdapter.notifyDataSetChanged();
                    } else {
                        ToastUtil.showToast(ForumDetailNewActivity.this, arrayListHttpResp.message);
                    }
                } else {
                    if (arrayListHttpResp != null) {

                        if (arrayListHttpResp.isOk()) {
                            if (arrayListHttpResp.data != null && arrayListHttpResp.data.size() > 0) {
                                mPage = page;
                                CommentList.addAll(arrayListHttpResp.data);
                                mForumCommentListAdapter.notifyDataSetChanged();
                            } else {
                                ToastUtil.showToast(ForumDetailNewActivity.this, getString(R.string.no_more_data));
                            }
                        } else {
                            ToastUtil.showToast(ForumDetailNewActivity.this, arrayListHttpResp.message);
                        }
                    } else {

                    }

                }
            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
//        MediaPlay.onPause();
        MediaPlay.onRelease();
        if (ad!=null) {
            ad.stop();
            commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);
        }
        if (ad1!=null) {
            ad1.stop();
            mFourumDetailView.setBackgroundResource(R.drawable.adj_voice);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
//        MediaPlay.onResume();
        if (ad!=null) {
            ad.stop();
            commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);
        }
        if (ad1!=null) {
            ad1.stop();
            mFourumDetailView.setBackgroundResource(R.drawable.adj_voice);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        MediaPlay.onRelease();
        if (mAudioButton != null) {
            mAudioButton.onDestroy();
        }
        if (ad!=null) {
            ad.stop();
            commentListAnimeView.setBackgroundResource(R.drawable.adj_voice);
        }
        if (ad1!=null) {
            ad1.stop();
            mFourumDetailView.setBackgroundResource(R.drawable.adj_voice);
        }
    }

}
